## -*- coding: utf-8 -*-

#   Copyright (C) 2007, 2008 Yves Junqueira (yves@cetico.org) 
#
#    This file is part of GeekDNS.
#
#    GeekDNS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    GeekDNS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GeekDNS.  If not, see <http://www.gnu.org/licenses/>.

# Based on the TurboGears's example (MIT?)

import turbogears
from geekroot import model
from sqlobject import *
hub = __connection__ = model.hub
from datetime import datetime
from turbogears import config
from registration.ormmanager import count

def create_registration_tables():
  "Create the appropriate database tables."
  RegistrationPendingUser.createTable(ifNotExists=True)
  RegistrationUserEmailChange.createTable(ifNotExists=True)

# Automatically create the registration tables when TurboGears starts up
turbogears.startup.call_on_startup.append(create_registration_tables)


_user_path = config.get('identity.soprovider.model.user',
                    'geekroot.model.User')
User = turbogears.util.load_class(_user_path)
                    

class RegistrationPendingUser(SQLObject):
  """Model to store information about users that have not been verified.
  """
  # This class should be modified to essentially look like your main model.User
  # class.  You don't need to add in the group information
  email_address = UnicodeCol( length=255, alternateID=True,
                 alternateMethodName="by_user_name" )
  full_name = UnicodeCol( length=255 )
  password = UnicodeCol( length=40 )
  created = DateTimeCol( default=datetime.now )
  validation_key = StringCol( length=40 )
  street_address = UnicodeCol(length=255)
  city = UnicodeCol(length=255)
  state = UnicodeCol(length=5)
  zipcode = UnicodeCol(length=15)
  country = UnicodeCol(length=255)
  phone1 = UnicodeCol(length=30)
  # these are optional
  phone2 = UnicodeCol(length=30)
  mobilephone = UnicodeCol(length=30)
  homepage = UnicodeCol(length=255)

class RegistrationUserEmailChange(SQLObject):
  """Model to store changes to user email addresses for validation.
  """
  user = ForeignKey(User.__name__)
  new_email_address = UnicodeCol(length=255, alternateID=True,
                  alternateMethodName='by_new_email_address')
  validation_key = StringCol(length=40)
  created = DateTimeCol(default=datetime.now)
  

     
def email_is_unique(email):
  "Return True if the email is not yet in the database."
  user_count = count(User, email_address=email)
  pending_count = count(RegistrationPendingUser, email_address=email)
  changed_count = count(RegistrationUserEmailChange, new_email_address=email)
  return not(user_count or pending_count or changed_count)
